﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using ParkingLot.Entitys;
using ParkingLot.Utility.DBConnection;
using ParkingLot.Utility.LogHelps;

namespace ParkingLot.Business.Entitys
{
    [Serializable]
    public abstract class CommonCollection : ViewModelBase
    {
        [XmlIgnore]
        public Action CurrentValueChangeAction;

        private int m_CurrentValue;

        /// <summary>
        /// 选中的值
        /// </summary>

        public int CurrentValue
        {
            get { return m_CurrentValue; }
            set
            {
                m_CurrentValue = value;
                this.RaisePropertyChanged(() => CurrentValue);
                if (CurrentValueChangeAction != null)
                {
                    CurrentValueChangeAction();
                }
            }
        }

        private List<OrderPare<int, string>> m_Sources = new List<OrderPare<int, string>>();
        
        [XmlIgnore]
        public List<OrderPare<int, string>> Sources
        {
            get { return m_Sources; }
            set { m_Sources = value; }
        }

        public CommonCollection()
        {
            //查询语句
            StringBuilder sbSql = GetSelectSql();
            //添加Order bY 语句
            AddOrderBySql(sbSql);
            //根据Sql语句 初始化数据源
            InitSource(sbSql);

            //赋初值
            SetDefultValue();
        }

        public CommonCollection(CommonCollection commonCollection)
        {
            InitSource(commonCollection);
            //赋初值
            SetDefultValue();
        }



        /// <summary>
        /// 获取查询的Sql语句
        /// </summary>
        /// <returns></returns>
        public abstract StringBuilder GetSelectSql();

        public virtual void AddOrderBySql(StringBuilder sbSql)
        {
            sbSql.AppendLine("ORDER BY 1");
        }

        /// <summary>
        /// 使用Sql语句初始化
        /// </summary>
        /// <param name="sbSql"></param>
        public virtual void InitSource(StringBuilder sbSql)
        {
            IDBSession dbSession = DbSessionFactory.GetDbsession();
            DataTable dt = dbSession.GetDataTable(sbSql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if ((!(dt.Rows[i]["ValueMember"] is DBNull)) && ((!(dt.Rows[i]["DisplayMember"] is DBNull))))
                {
                    OrderPare<int, string> orderPare = new OrderPare<int, string>();
                    orderPare.ValueMember = Convert.ToInt32(dt.Rows[i]["ValueMember"]);
                    orderPare.DisplayMember = dt.Rows[i]["DisplayMember"].ToString();
                    m_Sources.Add(orderPare);
                }
                else
                {
                    LogHelp.AddLog(new LogEncity($"获取字典失败,请检查如下Sql语句   {sbSql.ToString()}"));
                }
            }
        }

        /// <summary>
        /// 通过现有的类初始化
        /// </summary>
        /// <param name="commonCollection"></param>
        /// <param name="addALL">是否添加全部</param>
        public virtual void InitSource(CommonCollection commonCollection)
        {
            m_Sources = commonCollection.Sources;
        }

        /// <summary>
        /// 添加全部
        /// </summary>
        /// <param name="valueMember"></param>
        /// <param name="displayMember"></param>
        public virtual void AddAll(int valueMember = -1, string displayMember = "全部")
        {
            m_Sources.Insert(0, new OrderPare<int, string>() { ValueMember = valueMember, DisplayMember = displayMember });
            SetDefultValue();
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        public virtual void SetDefultValue()
        {
            var orderPare = m_Sources.FirstOrDefault();
            if (orderPare != null)
            {
                m_CurrentValue = m_Sources.FirstOrDefault().ValueMember;
            }
            else
            {
                m_CurrentValue = -1;
            }
            
        }
    }
}
